package com.jams.goodox.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;

import com.github.lzyzsd.library.BridgeWebView;
import com.jams.goodox.BaseActivity;
import com.jams.goodox.R;
import com.jams.goodox.TownApplicationLike;
import com.jams.goodox.entity.RepairFocusBean;
import com.jams.goodox.entity.resp.NewsDetailResp;
import com.jams.goodox.net.RetrofitCallbackListener;
import com.jams.goodox.net.services.EnterpriseService;
import com.jams.goodox.net.services.ServiceService;
import com.jams.goodox.utils.DelayUtils;
import com.jams.goodox.utils.StringUtils;
import com.jams.goodox.utils.StyleUtil;
import com.jams.goodox.utils.ToastUtil;
import com.jams.goodox.utils.Utils;
import com.jams.goodox.utils.WebViewUtils;

import butterknife.Bind;
import retrofit2.Call;

import static com.jams.goodox.activity.serve.PartyServiceFragment.RESULT_CODE_PARTY;
import static com.jams.goodox.activity.serve.PartyServiceFragment.TYPE_SERVICE;

/**
 * 用于统一处理h5模板形式展示页面
 * Created by jams_zhen on 2016/9/14.
 */
public class ScriptWebView extends BaseActivity implements BridgeWebView.LoadListener {
    @Bind(R.id.webView)
    BridgeWebView webView;
    @Bind(R.id.img_bottom)
    ImageView img_bottom;
    @Bind(R.id.img_bottom_out)
    ImageView img_bottom_out;
     
    public static  final String NEWS_DETAIL = "NEWS_DETAIL";
    @Override
    protected int getLayoutId() {
        return R.layout.service_webview;
    }

    @Override
    protected void init() {
        Intent in = getIntent();
        String barTitle = in.getStringExtra("barTitle");
        StyleUtil.titleBackKey(this, barTitle + "");

        String title = in.getStringExtra("title");

        String time = in.getStringExtra("time");
        String redNumbers = in.getStringExtra("num");
        String content = in.getStringExtra("content");

        webView.setLoadListener(this);
        final String type = in.getStringExtra(TYPE_SERVICE);
        String id = in.getStringExtra("id");
        // 如果有id直接从接口获取数据并展示，这样就可以显示最新的阅读数
        // 否则直接显示传过来的数据
        if (!TextUtils.isEmpty(id)) {
            if (!TextUtils.isEmpty(type)) {
                addReadCount(id, type);
            } else {
                addReadCount(id);
            }
        } else {//无id时，显示形式分仅内容及包含标题，时间的
            if (TextUtils.isEmpty(title) || TextUtils.isEmpty(time)) {
                WebViewUtils.setWebOnlyBody(webView, content + "");
            } else {
                if (TextUtils.isEmpty(redNumbers)) {//此处区分是否带阅读数的
                    WebViewUtils.setDataScriptListener(webView, title, time, content + "");
                } else {
                    WebViewUtils.setReadDataScriptListener(webView, title + "", time + "", "阅读数 " + redNumbers, content + "");
                }
            }

        }
    }

    private void addReadCount(String id) {
        showLoading();
        enqueue(createCallService(ServiceService.class).getFocus(id), new RetrofitCallbackListener<RepairFocusBean>() {
            @Override
            public void onSuccess(Call<RepairFocusBean> call, RepairFocusBean servicePeriod) {
                misLoading();
                RepairFocusBean.DataBean data = servicePeriod.getData();
                String title = data.getTitle();
                String intime = data.getIntime();
                int readnum = data.getReadnum();
                String content = data.getContent();
                if (readnum <= 0) {
                    WebViewUtils.setDataScriptListener(webView, "" + title, "" + intime, content + "");
                } else {
                    WebViewUtils.setReadDataScriptListener(webView, title + "", intime + "", "阅读数 " + readnum, content + "");
                }
            }

            @Override
            public void onError(String msg) {
                misLoading();
                ToastUtil.show(msg);
            }
        });
    }

    /**
     * ***热点,党员服务用到
     *
     * @param id   传入的id
     * @param type 传入的类型
     */
    private void addReadCount(final String id, final String type) {

        Call<NewsDetailResp> call;
        switch (type) {  //type "0":党务信息,"1":***热点
            case "0":
                call = createCallService(EnterpriseService.class).getPartyNewsDetail(id);
                break;
            case "1":
                call = createCallService(EnterpriseService.class).getTownNewsDetail(id);
                break;
            default:
                ToastUtil.show(mContext, "暂无数据");
                return;
        }
        showLoading();
        enqueue(call, new RetrofitCallbackListener<NewsDetailResp>() {
            @Override
            public void onSuccess(Call<NewsDetailResp> call, NewsDetailResp result) {
                try {
                    NewsDetailResp.NewsDetailData data = result.data;
                    String title = StringUtils.newString(data.title);
                    String intime = StringUtils.newString(data.createTime);
                    int readnum = data.readNum;
                    String content = StringUtils.newString(data.content);
                    if (readnum <= 0) {
                        WebViewUtils.setDataScriptListener(webView, "" + data.title, "" + data.createTime, content + "");
                    } else {
                        WebViewUtils.setReadDataScriptListener(webView, title + "", intime + "", "阅读数 " + readnum, content + "");
                        Intent i = new Intent();
                        i.putExtra(NEWS_DETAIL,readnum);
                        setResult(RESULT_CODE_PARTY,i);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onError(String msg) {
                misLoading();
            }
        });
    }

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        showLoading();
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        //获取加载完成后的h5页面高度，延迟避免部分手机获取不到webview高度
        DelayUtils.doAfterDelay(100, new DelayUtils.DelayDoListener() {
            @Override
            public void doAction() {
                int height = webView.getHeight();
                if (TownApplicationLike.height > height + Utils.dp2px(ScriptWebView.this, 190)) {
                    img_bottom_out.setVisibility(View.VISIBLE);
                    img_bottom.setVisibility(View.GONE);
                } else {
                    img_bottom_out.setVisibility(View.GONE);
                    img_bottom.setVisibility(View.VISIBLE);
                }
                misLoading();
            }
        });
    }

    @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {

    }
}
